//
//  ZHAppDelegate+ZHRegisterRoute.m
//  ZHRouter_Example
//
//  Created by 赵宏亚 on 2019/8/25.
//  Copyright © 2019 Arthur. All rights reserved.
//

#import "ZHAppDelegate+ZHRegisterRoute.h"
#import "ZHRouter.h"
#import "BeeHive.h"
#import "ZHBlogServiceProtocol.h"

@implementation ZHAppDelegate (ZHRegisterRoute)

- (void)registerRoute {
    
    
    /**
     ** 如果跳转页面需要参数，则用block类型自己跳转
     [[ZHRouter globalRouter] addPathComponent:@"blogvc" withName:@"ZHBlogServiceProtocol" handler:^(NSDictionary<NSString *,id> * _Nonnull params) {
     
     NSLog(@"params *** %@",params);
     }];
     
     ** 如果跳转页面不需要参数相关，可直接跳转
     [[ZHRouter globalRouter] addPathComponent:@"blogvc" withName:@"ZHBlogViewController"];
     */
        
    
    [[ZHRouter globalRouter] addPathComponent:@"blogvc" withName:@"ZHBlogViewController" handler:^(NSDictionary<NSString *,id> * _Nonnull params) {
        
        NSLog(@"params *** %@",params);
        UIViewController<ZHBlogServiceProtocol> *vc = [[BeeHive shareInstance] createService:@protocol(ZHBlogServiceProtocol)];
        [[self currentViewController].navigationController pushViewController:vc animated:YES];
    }];
    
}


// 获取当前vc
- (UIViewController *)currentViewController
{
    UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (viewController) {
        if ([viewController isKindOfClass:[UITabBarController class]]) {
            UITabBarController *tbvc = (UITabBarController*)viewController;
            viewController = tbvc.selectedViewController;
        } else if ([viewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *nvc = (UINavigationController*)viewController;
            viewController = nvc.topViewController;
        } else if (viewController.presentedViewController) {
            viewController = viewController.presentedViewController;
        } else if ([viewController isKindOfClass:[UISplitViewController class]] &&
                   ((UISplitViewController *)viewController).viewControllers.count > 0) {
            UISplitViewController *svc = (UISplitViewController *)viewController;
            viewController = svc.viewControllers.lastObject;
        } else  {
            return viewController;
        }
    }
    return viewController;
}

@end
